﻿/**
 * SlotDuino
 * 
 * Copyright (c) 2011/2012 Thiago Hummel Moreira, Ricardo Mizuuti, Rafael Mufato, Bruno Sakai, Andre Turina
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
using System.Windows;
using System.Windows.Controls;
using SlotDuino.DAL;
using SlotDuino.Sockets;
using SlotDuino.UI.Controls;
using System;
using System.Linq;
using System.Collections.Generic;

namespace SlotDuino.UI.Pages
{
    /// <summary>
    /// Interaction logic for RaceDashboard.xaml
    /// </summary>
    public partial class RaceDashboard : Page
    {
        #region Dependency properties
        public static readonly DependencyProperty TrackProperty = DependencyProperty.Register("Track", typeof(Track), typeof(RaceDashboard));
        #endregion
        
        #region Private attributes
        private EntityContainer _context;
        private SlotDaemonClient _client;
        private Dictionary<byte, SlotCard> _cards;
        #endregion

        #region Public properties
        public Track Track
        {
            get { return (Track)GetValue(TrackProperty); }
            set { SetValue(TrackProperty, value); }
        }

        public SlotDaemonClient Client
        {
            get
            {
                if (null == _client)
                {
                    _client = ((App)Application.Current).Client;
                }

                return _client;
            }
        }

        public Dictionary<byte, SlotCard> SlotCards
        {
            get { return _cards; }
        }
        #endregion
        
        public RaceDashboard()
        {
            InitializeComponent();
        }
        
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //Create the entity context
            _context = new EntityContainer();

            //Get the current track
            App application = (App)Application.Current;
            Track = application.GetCurrentTrack(_context);

            //Create the SlotCards
            _cards = new Dictionary<byte, SlotCard>(Track.Slots.Count);
            foreach (Slot slot in Track.Slots) {
                //Create a SlotCard
                var card = new SlotCard();
                card.Color = GetColor(slot.Color);
                card.Margin = new Thickness(5);
                card.Name = "Slot_" + slot.Id;

                _cards[slot.Id] = card;

                //Add it to grid
                pnlLanes.Children.Add(card);
            }

            //Keep a reference to the SlotDaemonClient
            _client = application.Client;

            //@TODO: Improve this
            //Check for mode
            UserControl raceControl;
            switch ((string)application.Properties["raceDashboardMode"])
            {
                case "Championship":
                    //@TODO: Improve this
                    var championshipId = (int)application.Properties["currentChampionshipId"];
                    var championship = _context.Championships.Single(c => c.Id == championshipId);

                    var championshipControl = new ChampionshipControl();
                    championshipControl.Context = _context;
                    championshipControl.Track = Track;
                    championshipControl.Championship = championship;

                    raceControl = championshipControl;
                    break;

                case "FreeRaces":
                default:
                    var freeRacesControl = new FreeRacesControl();
                    freeRacesControl.Context = _context;
                    freeRacesControl.Track = Track;
                    freeRacesControl.Client = _client;

                    raceControl = freeRacesControl;
                    break;
            }

            //Set control margins
            raceControl.Margin = new Thickness(0, 5, 0, 0);
            //Set control alignment
            raceControl.HorizontalAlignment = HorizontalAlignment.Left;
            //Insert the race control component
            layoutRoot.Children.Add(raceControl);
        }

        /// <summary>
        /// Utility function to convert a string to System.Windows.Media.Color
        /// </summary>
        /// <param name="color">String representing the color (name or RGB)</param>
        /// <returns>The Color object</returns>
        private System.Windows.Media.Color GetColor(string color)
        {
            var tmp = System.Drawing.Color.FromName(color);
            return System.Windows.Media.Color.FromArgb(tmp.A, tmp.R, tmp.G, tmp.B);
        }
    }
}
